-module(rpsgame3).
-export([play/1]).

%% Play a game of rock-paper-scissors
play(PlayerAttack) ->
  case lists:member(PlayerAttack, all_attacks()) of 
    true -> 
      ComputerAttack = get_computer_attack(),
      Result = get_result(PlayerAttack, ComputerAttack),
      {Result, {player, PlayerAttack}, {computer, ComputerAttack}};
    false ->
      {error, invalid_attack}
  end.

get_result(PlayerAttack, ComputerAttack) ->
  case {PlayerAttack, ComputerAttack} of
    {rock, scissors} -> win;
    {paper, rock} -> win;
    {scissors, paper} -> win;
    {Same, Same} -> draw;
    {_Ignored, _Ignored} -> lose
  end.
  
%% choose a computer attack at random
get_computer_attack() ->
  %% Get an index position at random
  Index = random:uniform(3),
  %% Pull out an attack  
  lists:nth(Index, [rock, paper, scissors]).

%% Get all possible attacks
all_attacks() ->
  [rock, paper, scissors].
  
